package org.lw.bootstrap.logic;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;

import javax.servlet.http.HttpServletRequest;

import org.lw.bootstrap.dto.Result;
import org.lw.bootstrap.dto.ResultFactory;

import com.alibaba.druid.support.logging.LogFactory;
import com.github.javafaker.Faker;

import net.javaw.torrent.TOTorrent;
import net.javaw.torrent.TOTorrentException;
import net.javaw.torrent.TOTorrentFactory;

/**
 *
 *
 */
public class AppLogic {

	private BufferedReader reader = null;

	/**
	 *
	 * @param request
	 * @return
	 */
	public Result tailFile(HttpServletRequest request, String path) {
		Map<String, String> result = new HashMap<String, String>();
		// result.put("content", readFileByLines(path));
		result = getInfo(readFileByLines(path));
		return ResultFactory.succeed(result);
	}

	/**
	 *
	 * @param request
	 * @return
	 */
	public Result closeTailFile(HttpServletRequest request, String path) {
		try {
			reader.close();
			reader = null;
		} catch (IOException e) {
			e.printStackTrace();
		}
		return ResultFactory.succeed();
	}

	/**
	 *
	 * @param request
	 * @return
	 */
	public Result getFakeInfo(HttpServletRequest request, String path) {
		Faker faker = new Faker();
		List<Map<String, Object>> resultList = new ArrayList<Map<String, Object>>();
		int dataRow = 10;
		int i = 0;
		while (i <= dataRow) {
			Map<String, Object> result = new HashMap<String, Object>();
			result.put("name", faker.name().name());
			result.put("phone", faker.phoneNumber().phoneNumber());
			result.put("company", faker.company().bs());
			result.put("country", faker.address().country());
			resultList.add(result);
			i++;
		}
		return ResultFactory.succeed(resultList);
	}

	/**
	 * @param fileName
	 * @return
	 */
	private String readFileByLines(String fileName) {
		String defaultFile = "D:\\git\\webtool\\src\\main\\data\\logdata.log";
		fileName = (fileName == null ? defaultFile : fileName);
		File file = new File(fileName);
		String tempString = null;
		try {
			if (reader == null) {
				reader = new BufferedReader(new FileReader(file));
			}

			// 一次读入一行，直到读入null为文件结束
			tempString = reader.readLine();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return tempString;
	}

	/**
	 * @param tempString
	 * @return
	 */
	private Map<String, String> getInfo(String tempString) {
		Map<String, String> result = new HashMap<String, String>();
		if (tempString == null) {
			return result;
		}
		String[] data = tempString.split(",");
		String topic = "";
		String timeline = "";
		String executeTime = "";
		for (String str : data) {
			if (str.contains("topic:")) {
				// 04-21 15:19:47.764 WARN
				// com.yom.cloud.ops.analyzer.report.AbstractReport -
				// topic-log-> topic: server-netevent-httpstatistic
				timeline = str.substring(0, str.indexOf("WARN")).trim();
				topic = str.substring(str.indexOf("topic:") + "topic:".length(), str.length()).trim();
			} else if (str.contains("ExecuteTime:")) {
				// ExecuteTime: 751
				executeTime = str.substring(str.indexOf("ExecuteTime:") + "ExecuteTime:".length(), str.length()).trim();
			}
		}

		result.put("timeline", timeline);
		result.put("topic", topic);
		result.put("executeTime", executeTime);
		return result;
	}
}
